﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ArtClub.DBAL;
using ArtClub.Controller;

namespace ArtClub.View
{
    public partial class AddAuction : System.Web.UI.Page
    {
        private string[] duration = { "3", "5", "10", "15", "20", "25", "30" };
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Request.QueryString["id"] != null)
            {
                PieceCtr control = new PieceCtr();
                Piece prod = control.getPiece(Convert.ToInt32(Request.QueryString["id"]));
                name.InnerHtml = prod.name;
                artist.InnerHtml = prod.artist;
                oPrice.InnerHtml = Convert.ToString(prod.originalPrice);
            }

           
            for (int i = 0; i <= 23; i++)
            {
                ListItem li = new ListItem();
                string value = "";
                if (i<10)
                {
                    value = "0" + i;
                }
                else
                {
                    value = i.ToString();
                }

                hour.Items.Add(new ListItem(value, value));
            }

            for (int i = 0; i <= 59; i++)
            {
                ListItem li = new ListItem();
                string value = "";
                if (i < 10)
                {
                    value = "0" + i;
                }
                else
                {
                    value = i.ToString();
                }
                min.Items.Add(new ListItem(value, value));
            }
            calendar.SelectedDate = DateTime.Today;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                AuctionCtr aCtr = new AuctionCtr();
                int id = Convert.ToInt32(Request.QueryString["id"]);
                string t = calendar.SelectedDate.ToString("dd/MM/yyyy") + " " + hour.SelectedValue + ":" + min.SelectedValue;
                DateTime stime = DateTime.ParseExact(t, "dd/MM/yyyy HH:mm", System.Globalization.CultureInfo.CurrentCulture);
                DateTime Jan1St1970 = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                double etime = (stime.AddMinutes(Convert.ToInt32(txtDuration.Text)) - Jan1St1970).TotalMilliseconds;
                
                aCtr.addAuction(id, stime, etime, Convert.ToInt32(txtStep.Text), Convert.ToDouble(txtStartPrice.Text));
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "asdf", "bootbox.alert('Art piece auction created successfully.')", true);
                cleanText();
            }
            catch (Exception exc)
            {

                Page.ClientScript.RegisterClientScriptBlock(GetType(), "asdf", "bootbox.alert('Create Auction failed.')", true);
                cleanText();
            }
            
        }

        private void cleanText()
        {
            txtDuration.Text = txtStartPrice.Text = txtStep.Text = string.Empty;
            hour.SelectedIndex = 0;
            min.SelectedIndex = 0;
        }

        

        
    }
}